
////////////////////////////////////////////////////////////////////////////////////////////
// 文件名: SysTBViewDAL.cs
//
// 模块名称: SysTBView
//
// 作者: yqf
//
// 历史记录（记录修改记录，修改文件后请添加修改记录，注明修改时间、修改人，修改内容）:
// 2013-01-24    yqf    创建文件
////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using HIDB;
using Model;
using Common;
using System.Configuration;

namespace DAL
{
    /// <summary>
    /// SysTBView表数据访问层
    /// </summary>
    public partial class SysTBViewDAL : DB
    {
        public SysTBViewDAL(LoginInfo info)
        {
            base.loginInfo = info;
        }
        public bool CheckViewData(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                IDataParameter[] queryParas = new SqlParameter[1];
                queryParas[0] = new SqlParameter("@sql", sql);
                HIDB.dbo con = base.ViewCon();
                //HIDB.dbo con = HIDB.common.make(loginInfo.ETLTargetType, string.Format("Server={0};database={1};uid={2};pwd={3};",
                //    DBCon.DbAddress, DBCon.DbName, loginInfo.Dbuser, loginInfo.Dbpwd));
                dt = con.StoredDataTable("Working_db.UP_SysView_CheckViewData", queryParas);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (dt != null && dt.Rows.Count > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 更新SysTBView表记录
        /// </summary>
        /// <param name="SysTBView">SysTBView实体对象</param>
        public void Update(IDataParameter[] sqlparams, string proc)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                //HIDB.dbo con = HIDB.common.make(loginInfo.ETLTargetType, string.Format("Server={0};database={1};uid={2};pwd={3};",
                //    DBCon.DbAddress, DBCon.DbName, loginInfo.Dbuser, loginInfo.Dbpwd));
                con.StoredUpdate("Working_db.Up_SysView_" + proc, sqlparams);
            }
            catch (Exception ex) { throw ex; }
        }
        public void SysView_OnInit()
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                IDataParameter[] queryParas = new SqlParameter[1];
                queryParas[0] = new SqlParameter("@SysCode", loginInfo.ETLTargetCode);
                con.StoredUpdate("Working_db.Up_SysView_OnInit", queryParas);
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 根据ID字段删除SysTBView表信息
        /// </summary>
        /// <param name="id">ID</param>
        public int Delete(string id)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                SqlParameter[] sqlparams = { new SqlParameter("@ID", id) };
                //执行操作
                DataTable dt = con.StoredDataTable("Working_db.UP_SysView_Delete", sqlparams);
                if (dt != null && dt.Rows.Count > 0)
                {
                    return Convert.ToInt32(dt.Rows[0][0]);
                }
                else
                    return 0;
            }
            catch (Exception ex) { throw ex; }
        }
        public List<string> EditPushName(string id)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                SqlParameter[] sqlparams = { new SqlParameter("@ID", id) };
                //执行操作
                DataTable dt = con.GetDataTable("select PushName,PushSchema from [Working_db].SysView_Config where  id=@ID", sqlparams);
                if (dt != null && dt.Rows.Count > 0)
                {
                    List<string> list = new List<string>();
                    list.Add(dt.Rows[0][0].ToString());
                    list.Add(dt.Rows[0][1].ToString());
                    return list;
                }
                else
                    return new List<string>();
            }
            catch (Exception ex) { throw ex; }
        }
        public void SavePushName(string id, string pushName, string pushSchema)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                SqlParameter[] sqlparams = { 
                                               new SqlParameter("@ID", id), 
                                               new SqlParameter("@PushName", pushName),
                                               new SqlParameter("@PushSchema", pushSchema)
                                           };
               
                con.StoredDataTable("Working_db.UP_SysView_SavePushName", sqlparams);
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 根据ID字段删除Push表信息
        /// </summary>
        /// <param name="id">ID</param>
        public bool DeletePush(string id)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                SqlParameter[] sqlparams = { new SqlParameter("@ID", id) };

                con.StoredUpdate("Working_db.UP_SysView_DeletePush", sqlparams);
                return true;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 获取SysTBView对象列表
        /// </summary>
        public SysTBView GetItem(string id)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                SysTBView model = new SysTBView();
                DataTable dt = new DataTable();
                IDataParameter[] paras = new SqlParameter[2];
                paras[0] = new SqlParameter("@ID", id);
                paras[1] = new SqlParameter("@Type", "0");
                dt = con.StoredDataTable("Working_db.UP_SysView_GetItem", paras);

                if (dt != null && dt.Rows.Count > 0)
                {
                    model.ID = dt.Rows[0]["ID"].ToString();
                    model.ViewName = dt.Rows[0]["ViewName"].ToString();
                    model.ViewType = Convert.ToInt32(dt.Rows[0]["ViewType"]);
                    model.Definition = dt.Rows[0]["Definition"].ToString();
                    model.Description = dt.Rows[0]["Description"].ToString();
                    model.TopNum = Convert.ToInt32(dt.Rows[0]["TopNum"]);
                    model.Status = Convert.ToInt32(dt.Rows[0]["Status"]);
                    model.JoinType = dt.Rows[0]["JoinType"].ToString();
                }
                return model;
            }
            catch (Exception ex) { throw ex; }
        }
        public DataTable GetItem(string id, int type)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                SysTBView model = new SysTBView();
                IDataParameter[] paras = new SqlParameter[2];
                paras[0] = new SqlParameter("@ID", id);
                paras[1] = new SqlParameter("@Type", type);
                return con.StoredDataTable("Working_db.UP_SysView_GetItem", paras);
            }
            catch (Exception ex) { throw ex; }
        }
        public string GetCreateTbScript(string viewID, string etlType)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                string msg = string.Empty;
                DataTable dt = new DataTable();
                IDataParameter[] queryParas = new SqlParameter[2];
                queryParas[0] = new SqlParameter("@ViewID", viewID);
                queryParas[1] = new SqlParameter("@type", etlType);
                dt = con.StoredDataTable("Working_db.Create_TargetSystem_CreateTbScript", queryParas);

                if (dt != null && dt.Rows.Count > 0)
                {
                    msg = dt.Rows[0][0].ToString();

                }
                return msg;
            }
            catch (Exception ex) { throw ex; }
        }
        public DataTable ExportData(string id)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                SysTBView model = new SysTBView();
                DataTable dt = new DataTable();
                SqlParameter[] sqlparams = { new SqlParameter("@ID", id) };
                dt = con.StoredDataTable("Working_db.UP_SysView_ExportData", sqlparams);
                return dt;
            }
            catch (Exception ex) { throw ex; }
        }
        public string GetAuditDetail(string id)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                string msg = string.Empty;
                DataTable dt = new DataTable();
                SqlParameter[] sqlparams = { new SqlParameter("@ID", id) };
                dt = con.StoredDataTable("Working_db.UP_SysView_AuditDetail", sqlparams);

                if (dt != null && dt.Rows.Count > 0)
                {
                    msg = dt.Rows[0][0].ToString();

                }
                return msg;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 获取SysTBView对象列表
        /// </summary>
        public List<SysTBView> GetList(IDataParameter[] sqlparams)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                List<SysTBView> list = new List<SysTBView>();
                DataTable dt = new DataTable();
                //IDataParameter idp = new SqlParameter("@SysCode", loginInfo.ETLTargetCode);
                //sqlparams.AddOne(idp);
                //sqlparams.SetValue(idp, sqlparams.Length-1);
                dt = con.StoredDataTable("Working_db.UP_SysView_GetList", sqlparams);

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {

                        SysTBView model = new SysTBView();

                        model.ID = row["ID"].ToString();
                        model.IDs = row["IDs"].ToString();
                        model.ViewGroup = row["GroupName"].ToString();
                        model.Schema = row["ViewSchema"].ToString();
                        model.ViewName = row["ViewName"].ToString();
                        model.PushName = row["PushName"].ToString();
                        model.ViewType = Convert.ToInt32(row["ViewType"]);
                        model.Status = Convert.ToInt32(row["Status"]);

                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex) { throw ex; }
        }
        public List<SysViewPush> GetPushList(IDataParameter[] sqlparams)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                List<SysViewPush> list = new List<SysViewPush>();
                DataTable dt = new DataTable();

                dt = con.StoredDataTable("Working_db.UP_SysView_GetPushList", sqlparams);

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {

                        SysViewPush model = new SysViewPush();

                        model.ID = row["ID"].ToString();
                        model.GroupName = row["GroupName"].ToString();
                        model.ViewGroupID = row["ViewGroupID"].ToString();
                        model.ViewName = row["ViewName"].ToString();
                        model.ViewType = row["ViewType"].ToString();
                        model.PushType = row["PushType"].ToString();
                        model.IsFullAmount = row["IsFullAmount"].ToString();
                        model.IsDisable = row["IsDisable"].ToString();
                        model.DeletedType = row["DeletedType"].ToString();

                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex) { throw ex; }
        }
        public DataTable PushConfig(IDataParameter[] sqlparams)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                List<SysTBView> list = new List<SysTBView>();
                DataTable dt = new DataTable();
                dt = con.StoredDataTable("Working_db.UP_SysView_PushConfig", sqlparams);

                return dt;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 获取ApproveList对象列表
        /// </summary>
        public List<SysViewApprove> ApproveList(IDataParameter[] sqlparams)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                DataTable dt = new DataTable();
                List<SysViewApprove> list = new List<SysViewApprove>();

                dt = con.StoredDataTable("Working_db.UP_SysView_ApproveList", sqlparams);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {

                        SysViewApprove model = new SysViewApprove();
                        model.GUID = row["GUID"].ToString();
                        model.GroupName = row["GroupName"].ToString();
                        model.ViewName = row["ViewName"].ToString();
                        model.ViewType = row["ViewType"].ToString();
                        model.AuditTime = Convert.ToDateTime(row["AuditTime"]).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(row["AuditTime"]).ToLongTimeString();
                        model.AuditStatus = Convert.ToInt32(row["AuditStatus"].ToString());
                        model.Status = Convert.ToInt32(row["Status"]);

                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 获取SysTBView对象总数
        /// </summary>
        public int GetListCount(IDataParameter[] sqlparams)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                DataTable dt = new DataTable();
                sqlparams.AddOne(new SqlParameter("@SysCode", loginInfo.ETLTargetCode));
                dt = con.StoredDataTable("Working_db.UP_SysView_GetCount", sqlparams);

                if (dt != null && dt.Rows.Count > 0)
                {
                    return Int32.Parse(dt.Rows[0][0].ToString());
                }
                else
                    return 0;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 获取GetPushCount对象总数
        /// </summary>
        public int GetPushCount(IDataParameter[] sqlparams)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                DataTable dt = new DataTable();
                sqlparams.AddOne(new SqlParameter("@SysCode", loginInfo.ETLTargetCode));
                dt = con.StoredDataTable("Working_db.UP_SysView_GetPushCount", sqlparams);

                if (dt != null && dt.Rows.Count > 0)
                {
                    return Int32.Parse(dt.Rows[0]["Count"].ToString());
                }
                else
                    return 0;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 获取ApproveList对象总数
        /// </summary>
        public int ApproveListCount(IDataParameter[] sqlparams)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                DataTable dt = new DataTable();
                sqlparams.AddOne(new SqlParameter("@SysCode", loginInfo.ETLTargetCode));
                dt = con.StoredDataTable("Working_db.UP_SysView_ApproveListCount", sqlparams);

                if (dt != null && dt.Rows.Count > 0)
                {
                    return Int32.Parse(dt.Rows[0]["Count"].ToString());
                }
                else
                    return 0;
            }
            catch (Exception ex) { throw ex; }
        }
        public List<Test_BaseView> GetBaseViewList(string username)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                List<Test_BaseView> list = new List<Test_BaseView>();
                DataTable dt = new DataTable();
                IDataParameter[] paras = new SqlParameter[1];
                paras[0] = new SqlParameter("@SysCode", loginInfo.ETLTargetCode);
                dt = con.StoredDataTable("Working_db.UP_GetBaseViewList", paras);

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {

                        Test_BaseView model = new Test_BaseView();

                        model.ID = row["ID"].ToString().Replace(".", "_");
                        model.Name = row["Name"].ToString();
                        model.EnName = row["EnName"].ToString();

                        list.Add(model);
                    }
                }
                return list;
            }
            catch (Exception ex) { throw ex; }
        }
        public DataSet SavedView(string id, string index)
        {
            try
            {
                HIDB.dbo con = base.ViewCon();
                DataSet ds = new DataSet();
                ds = con.GetDataSet(string.Format("exec [Working_db].[UP_SysView_GetConfig] '{0}',{1}", id, index));
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds;
                }
                else
                    return new DataSet();
            }
            catch (Exception ex) { throw ex; }
        }
    }
}